package Tools;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class LoginTools {
	private static final String url = "jdbc:mysql://localhost:3306/ds?useSSL=false&serverTimezone=UTC";
	private static final String user = "root";
	private static final String password = "123456";
	/**加载驱动*/
	static {
		
		try{
			DriverManager.registerDriver( new com.mysql.cj.jdbc.Driver());
		}catch (SQLException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 获取数据库连接的方法
	 * @return返回数据库连接对象
	 */
	public static Connection getConnection(){
		Connection conn = null;
		try {
				conn = DriverManager.getConnection(url,user, password);
		}catch (SQLException e){
				e.printStackTrace();
		}
		return conn;
	}
	/**
	 * 关闭数据库相关连接
	 * @param conn  连接对象
	 * @param ps    声明对象
	 * @param rs    结果集对象
	 */
	public static void closeResource(Connection conn, PreparedStatement ps, ResultSet rs) {
		try {
			if(rs != null && !rs.isClosed()) {
				rs.close();
			}
		}catch(SQLException e) {
			e.printStackTrace();
		}
		try {
			if(ps != null && !ps.isClosed()) {
				ps.close();
			}
		}catch(SQLException e) {
			e.printStackTrace();
		}
		try {
			if(conn != null && !conn.isClosed()) {
				conn.close();
			}
		}catch(SQLException e) {
			e.printStackTrace();
		}
	}
}
